package com.qms.repast.ui.presenter;

import android.app.Activity;

import com.google.gson.Gson;
import com.orhanobut.logger.Logger;
import com.qms.repast.commons.Constants;
import com.qms.repast.entity.resbean.UserInfo;
import com.qms.repast.module.UserModule;
import com.qms.repast.ui.base.BasePresenter;
import com.qms.repast.ui.view.ILoginView;
import com.qms.repast.utils.AppUtils;
import com.qms.repast.utils.SpUtils;
import com.qms.repast.utils.UserSpUtils;

import java.util.Map;

import io.reactivex.observers.DisposableObserver;

/**
 * @author 泅渡者
 * @date 2018/8/31 0031
 * @describe:
 */
public class LoginPresenter extends BasePresenter<ILoginView> {

	protected UserModule userModule;

	public LoginPresenter (Activity mContext, ILoginView mView) {
		super(mContext, mView);
		this.userModule = new UserModule(mContext);
	}

	/**
	 * 登陆
	 *
	 * @param params
	 */
	public void getLogin (Map<String, String> params) {
		userModule.reqLogin(params, new DisposableObserver<UserInfo>() {
			@Override
			public void onError (Throwable throwable) {
				Logger.e(throwable.getMessage());
			}

			@Override
			public void onComplete () {
				Logger.d("Request is End");
			}

			@Override
			public void onNext (UserInfo userInfo) {
				Logger.json(new Gson().toJson(userInfo));
				if (Constants.OLDRESULTCODE.equals(userInfo.getHeader().getStatusCode())) {
					SpUtils.setParam(AppUtils.getContext(), Constants.SESSION, userInfo.getBody().getUserNameLogin().getSession());
					SpUtils.setParam(AppUtils.getContext(), Constants.USERID, userInfo.getBody().getUserNameLogin().getId());
					UserSpUtils.saveUserInfo(AppUtils.getContext(), new Gson().toJson(userInfo));
					mView.showInfo(userInfo);
				}
			}
		});
	}
}
